﻿using System;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductPackage_CheckandUnion : System.Web.UI.Page
{
    private static string var_productSN, var_packagePrdouctSN, var_SN3BK, var_productTypeID;
    private static PackageProduct pp;
    private static TestInfo ti;

    protected void Page_Load(object sender, EventArgs e)
    {
        //this.Form.DefaultButton = "Button2";
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        pp = new PackageProduct();       
        var_productSN = this.TextBox1.Text.Trim();
        if (string.IsNullOrEmpty(IsUnionProductSN(var_productSN)))
        {
            string str1 = "序列号不正确，或者是没有关联过的成品序列号！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");  
        }
        else
        {
            ti = pp.GetlastTest(IsUnionProductSN(var_productSN));
            if (ti.IsPass == "Pass")
            {
                this.Image1.Visible = true;
                this.Image2.Visible = false;
            }
            else if (ti.IsPass == "Fail")
            {
                this.Image2.Visible = true;
                this.Image1.Visible = false;
            }
            else
            {
                string str = "没有相关的测试记录！"; 
                //this.Label1.Visible = true;
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
                this.Image1.Visible = false;
                this.Image2.Visible = false;
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        pp = new PackageProduct();
        var_productSN = this.TextBox1.Text.Trim();
        var_packagePrdouctSN = this.TextBox2.Text.Trim();
        var_SN3BK = this.TextBox3.Text.Trim();
        var_productTypeID = this.TextBox4.Text.Trim();

        if (string.IsNullOrEmpty(IsUnionProductSN(var_productSN)))
        {
            string str1 = "序列号不是正确的腔体序列号，或者是没有关联过的成品序列号！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
        }
        else if (IsUnionProductSN(var_productSN) != var_productSN)
        {
            string str6 = "查询的序列号是已经关联过的成品序列号，不能再作为腔体序列号关联！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str6 + "');</script>");
        }
        else
        {
            //PackageProductInfo ppi = pp.GetPackageProductbyPackageProductSN(var_packagePrdouctSN);
            //PackageProductInfo ppi1 = pp.GetPackageProductbyProductSN(var_productSN);
            if (pp.GetPackageProductbyProductSN(var_productSN).ProductSN == var_productSN)
            {
                string str5 = "已经存在此腔体条码的包装数据，不能重复包装！";
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str5 + "');</script>");
            }
            else if (pp.GetPackageProductbyPackageProductSN(var_packagePrdouctSN).PackageProductSN == var_packagePrdouctSN)
            {
                string str7 = "已经存在此成品序列号的包装数据，不能重复包装！";
                //this.Label2.Visible = true;
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str7 + "');</script>");
            }
            else
            {
                ti = pp.GetlastTest(var_productSN);
                //if (pp.Get3BK(ti.ProductTypeID) != var_SN3BK)
                //{
                //    string str8 = "产品的3BK条码不正确，请检查对应关系";
                //    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str8 + "');</script>");
                //}
                if (pp.Isvalid3bk(var_productTypeID, var_SN3BK) != "1")
                {
                    string str8 = "产品的3BK条码不正确，请检查对应关系";
                    ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str8 + "');</script>");
                }
                else
                {
                    if (ti != null)
                    {
                        if (ti.IsPass == "Pass")
                        {
                            string str2 = "关联成功";
                            DateTime dt = DateTime.Now;
                            string userID = User.Identity.Name;
                            PackageProductInfo ppinfo = new PackageProductInfo(var_packagePrdouctSN, var_productSN, var_SN3BK, userID, dt);
                            pp.Insert(ppinfo);
                            //this.Label3.Visible = true;
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str2 + "');</script>");
                        }
                        else
                        {
                            string str3 = "测试未通过的产品不能关联成品序列号";
                            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str3 + "');</script>");
                        }
                    }
                    else
                    {
                        string str4 = "请先查询此编号腔体的测试是否通过";
                        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str4 + "');</script>");
                    }
                }
            }
        }
    }

    protected string IsUnionProductSN(string productSN)
    {
        PackageProduct pp1 = new PackageProduct();
        return pp1.IsUnionProductSN(productSN);
    }
}
